package submitter;

use Exporter;
use Socket;
use viable_socket;
use Util::Logger;

our @ISA = qw( Exporter );
our @EXPORT = qw( sumbit_flags );

$gl=new Util::Logger('getters/global_log.txt');

sub make_socket_error
{
    my $msg=shift;
    $gl->error("ERROR In SUBMITTER: $msg");

    return undef;
}

sub make_socket
{
    my $sock;
    my $remote  = 'localhost';
    my $port    = 1337;  # random port
    my $iaddr   = inet_aton($remote) or return make_socket_error("no host: $remote");
    my $paddr   = sockaddr_in($port, $iaddr);

    my $proto   = getprotobyname('tcp');
    socket($sock, PF_INET, SOCK_STREAM, $proto)  or return make_socket_error("socket: $!");
    connect($sock, $paddr) or return make_socket_erorr("connect: $!");

    select $sock;
    $|++;
    select STDOUT;

    print "SOCK: $sock\n";
    return $sock;
}

sub new
{
    my ($self)=@_;
    my $s=new viable_socket(\&make_socket);
    my $obj={
	    sock => $s,
	};
    print "BLESS\n";
    bless $obj;
}

sub submit_flags
{
    print "SF0\n";
    my $self=shift;
    my $s=$self->{sock};
    my $flags=shift;
    my $ret=[];
    my $i=0;

    for(@$flags)
    {
	print "SF1\n";
	$s->print("$_\n");
	print "SF2\n";
	push (@$ret,$_) if ($i);
	$i=($i+1)%4;
    }
    return $ret;
    print "SF_END\n";
}

1;

